package javaweb.tail;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.*;
import javaweb.tail.util.DBUtil;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

@WebServlet({"/user/login","/user/exit"})
public class UserServlet extends HttpServlet {
    /**
     * 使用模板方法模式实现登录和退出检验
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        String servletPath = request.getServletPath();
        if("/user/login".equals(servletPath)){
            doLogin(request,response);
        }else if("/user/exit".equals(servletPath)){
            doExit(request,response);
        }
    }
    private void doExit(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException{

        HttpSession session = request.getSession(false);
        if(session!=null){
            session.invalidate();
        }
        response.sendRedirect(request.getContextPath()+"/index.jsp");
    }

    private void doLogin(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException{
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        //设置一个标志变量，判断是否登录成功
        boolean flag = false;
        try{
            conn = DBUtil.getConnection();
            String sql = "select * from user where username=? and password=?";
            ps = conn.prepareStatement(sql);
            ps.setString(1,username);
            ps.setString(2,password);
            rs = ps.executeQuery();
            if(rs.next()){
                flag = true;
            }

        }catch(SQLException e){
            e.getClass().getName();
        }finally{
            DBUtil.close(conn,ps,rs);
        }
        if(flag){
            // 登录成功跳转到“我的通讯录”页面
            // 登录成功建立一个session会话对象，让后面只有登录成功了的才能访问通讯录
            HttpSession session = request.getSession();
            session.setAttribute("username",username);

            if(request.getParameter("res")!=null&&request.getParameter("res").equals("1")) {
                Cookie cookie1 = new Cookie("username", username);
                Cookie cookie2 = new Cookie("password", password);
                // 设置cookie“生命周期”
                cookie1.setMaxAge(60 * 60 * 24 * 7);
                cookie2.setMaxAge(60 * 60 * 24 * 7);
                //设置cookie的路径
                cookie1.setPath(request.getContextPath());
                cookie2.setPath(request.getContextPath());
                //打入到响应对象里，响应到浏览器上让其保存起来
                response.addCookie(cookie1);
                response.addCookie(cookie2);
            }


            response.sendRedirect(request.getContextPath()+"/mytail.jsp");
        }else{
            // 登录失败，请重新登录
            response.sendRedirect(request.getContextPath() + "/error.jsp");
        }


    }
}
